<?php
require_once '../../../config/common_config.php';
require_once PATH_ROOT.'/inc/global.php';
require_once('.config.inc.php');
$db = createMysqlDbLink($mysql_config["of"]);
ini_set('date.timezone','Etc/GMT');

require_once('.config.inc.php');




$account='Zity-US';
$plantime=strtotime('2017-01-01 00:00:00');echo "抓取开始时间是".date("Y-m-d H:i:s",$plantime)."<br>";
$plantimeend=strtotime('2017-02-01 00:00:00');echo "抓取结束时间是".date("Y-m-d H:i:s",$plantimeend)."<br>";
	$author=$db->getValue("select `author` from `auth` where `source` like 'amazon' and `account` like '".$account."'");
	$auth=json_decode($author,true);
	$MARKETPLACE_ID=$auth['MARKETPLACE_ID'];
 $config = array (
   'ServiceURL' => $serviceUrl,
   'ProxyHost' => null,
   'ProxyPort' => -1,
   'ProxyUsername' => null,
   'ProxyPassword' => null,
   'MaxErrorRetry' => 3,
 );

 $service = new MarketplaceWebServiceOrders_Client(
        $auth['AWS_ACCESS_KEY_ID'],
        $auth['AWS_SECRET_ACCESS_KEY'],
        APPLICATION_NAME,
        APPLICATION_VERSION,
        $config);

 $request = new MarketplaceWebServiceOrders_Model_ListOrdersRequest(); 
 $request->setSellerId($auth['MERCHANT_ID']); 

 $request->setMarketplaceId(array($auth['MARKETPLACE_ID']));

 $request->setLastUpdatedAfter(date("Y-m-d\TH:i:s\Z",$plantime));echo date("Y-m-d H:i:s",$plantime)."<br>";
 $request->setLastUpdatedBefore(date("Y-m-d\TH:i:s\Z",$plantimeend));echo date("Y-m-d H:i:s",$plantimeend);
 $request->setFulfillmentChannel(array('AFN','MFN'));
 $request->setOrderStatus(array('Shipped','Unshipped','PartiallyShipped','Pending','Canceled'));


 invokeListOrders($service, $request);

/**
  * Get List Order Items Action Sample
  * Gets competitive pricing and related information for a product identified by
  * the MarketplaceId and ASIN.
  *
  * @param MarketplaceWebServiceOrders_Interface $service instance of MarketplaceWebServiceOrders_Interface
  * @param mixed $request MarketplaceWebServiceOrders_Model_ListOrderItems or array of parameters
  */

  function invokeListOrders(MarketplaceWebServiceOrders_Interface $service, $request)
  {
      try {
        $response = $service->ListOrders($request);
		$dom = new DOMDocument();
        $dom->loadXML($response->toXML());
        $dom->preserveWhiteSpace = false;
        $dom->formatOutput = true;
       // echo("ResponseHeaderMetadata: " . $response->getResponseHeaderMetadata() . "\n");
	    $data=$dom->saveXML();
		$xml = simplexml_load_string($data);
		$data1 = json_decode(json_encode($xml),TRUE); //print_r($data1);
		print_r($data1);

        echo ("Service Response\n");
        echo ("=============================================================================\n");

        $dom = new DOMDocument();
        $dom->loadXML($response->toXML());
        $dom->preserveWhiteSpace = false;
        $dom->formatOutput = true;
        echo $dom->saveXML();
        echo("ResponseHeaderMetadata: " . $response->getResponseHeaderMetadata() . "\n");

     } catch (MarketplaceWebServiceOrders_Exception $ex) {
        echo("Caught Exception: " . $ex->getMessage() . "\n");
        echo("Response Status Code: " . $ex->getStatusCode() . "\n");
        echo("Error Code: " . $ex->getErrorCode() . "\n");
        echo("Error Type: " . $ex->getErrorType() . "\n");
        echo("Request ID: " . $ex->getRequestId() . "\n");
        echo("XML: " . $ex->getXML() . "\n");
        echo("ResponseHeaderMetadata: " . $ex->getResponseHeaderMetadata() . "\n");
     }
 }
